

*************************************************************************************
*****			Please cite Georgiadou et al. (2018) as indicated in 			*****
*****  	https://www.sciencedirect.com/science/article/pii/S026137941830026X		*****
*************************************************************************************



use Data/GRR_data.dta, clear
cd Tables


***** Table 1: Summary statistics
su FR PRR ER Lunemp Limmigration_cr Ldiff_imm Lgini_disp_inc Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par if FR!=.


******** Table 2 (requires Honoré's pantob.ado: https://www.princeton.edu/~honore/stata/index.html
** Pseudo R2s are calculated separately for each column in file "GRR_goodness_of_fit.do"

** Column 1
pantob PRR Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, replace ctitle(radical-pantobit) keep(Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 2
pantob ER Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(extreme pantob) keep(Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 3
pantob PRR Lunemp Limmigration_cr Lunemp_immigr_cr Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(radical-pantobit) keep(Lunemp Limmigration_cr Lunemp_immigr_cr Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 4
pantob ER Lunemp Limmigration_cr Lunemp_immigr_cr Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(extreme pantob) keep(Lunemp Limmigration_cr Lunemp_immigr_cr Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 5
pantob PRR Lunemp Ldiff_immigration Lunemp_diffimmigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(radical-pantobit) keep(Lunemp Ldiff_immigration Lunemp_diffimmigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 6
pantob PRR Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(radical-pantobit) keep(Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 7
pantob ER Lunemp Ldiff_immigration Lunemp_diffimmigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table2.xls, append ctitle(extreme pantob) keep(Lunemp Ldiff_immigration Lunemp_diffimmigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3) sortvar(Lunemp Limmigration_cr Lunemp_immigr_cr Ldiff_immigration Lunemp_diffimmigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par)





******** Table 3 
** Pseudo R2s are calculated separately for each column in file "GRR_goodness_of_fit.do"

** Column 1
pantob PRR Lgini_disp_inc Lunemp Ldiff_immigration D_par nuts_2 
outreg2 using table3.xls, replace ctitle(radical-pantobit) keep(Lgini_disp_inc Lunemp Ldiff_immigration D_par) addtext(Region FE, yes) label dec(3)
** Column 2
pantob ER Lgini_disp_inc Lunemp Ldiff_immigration D_par nuts_2 
outreg2 using table3.xls, append ctitle(extreme pantob) keep(Lgini_disp_inc Lunemp Ldiff_immigration D_par) addtext(Region FE, yes) label dec(3)
** Column 3
pantob PRR Lgini_disp_inc Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table3.xls, append ctitle(radical-pantobit) keep(Lgini_disp_inc Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)
** Column 4
pantob ER Lgini_disp_inc Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 
outreg2 using table3.xls, append ctitle(extreme pantob) keep(Lgini_disp_inc Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par) addtext(Region FE, yes) label dec(3)



******* table 4
***** immigration flows
*PRR
tobit PRR Lunemp Ldiff_immigration Lgdp_pps_hab i.nuts_2, ll(0) //run Tobit with dummies and statistically significant regressors in order to calculate fitted values. 
gen sample_1=e(sample) // keep sample to obtain mean/medians for sample used
*predict ystar, ystar(0,1)
**gen touse = e(sample)
*replace touse=0 if missing(ystar) 
margins, dydx(Lunemp Ldiff_immigration Lgdp_pps_hab) predict(e(0,.)) // get prediction conditional on uncensored sample
su PRR Lunemp Ldiff_immigration if sample_1==1,d //get means and medians. Calculation of policy effects is done by hand in file "policy table.xlsx"


*ER
tobit ER Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lwage_sh Lgrowth D_par i.nuts_2,ll(0) //run Tobit with dummies and statistically significant regressors in order to calculate fitted values.
gen sample_2=e(sample) // keep sample to obtain mean/medians for sample used
*predict ystar, ystar(0,1)
margins, dydx(Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lwage_sh Lgrowth D_par) predict(e(0,.)) // get prediction conditional on uncensored sample

su ext_right_cat1 Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lwage_sh Lgrowth D_par if sample_2 //get means
su ext_right_cat1 Lunemp Ldiff_immigration Lgdp_pps_hab Ltax Lwage_sh Lgrowth D_par if sample_2,d //get means and medians. 
*Calculation of policy effects is done by hand in file "policy_table.xlsx"





******* table 5
************** SAVE DATABASE FOR R-ECOLOGICAL INFERENCE


*** save sample for unemployment ecological regression
rename Lunemp unemployed // use lagged unemployed (if elections are held towards the end of the year current unemployment might be preferrable: results remain unchanged if we use current unemployment)
gen employed=1-unemployed // generate employment rate
***** keep non-missing observations for FR and unemployed/employed

pantob PRR unemployed Limmigration_cr Lunemp_immigr_cr Lgdp_pps_hab Ltax Lgini Lwage_sh LD_gdp_growth D_par nuts_2 //use sample of basic regression for compatibility (results qualitatively unchanged with whole sample -- differences only in 3rd decimal point of ecological estimation) 
gen Dsample_unemp=e(sample)
keep if Dsample_unemp==1

keep nuts_2 year country PRR ER unemployed employed validvotes Dsample_unemp
 
cd ..
cd Data
saveold ecological_unemployment.dta, version(12) replace // save database to open in R for ecological regression. Ecological regression is done in R, code provided in file "Table5_FigureA4_ecological.R"
cd ..










 
